package com.darly.book.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.darly.book.R;
import com.darly.book.model.bean.SectionBean;
import com.darly.book.model.flag.FindType;
import com.darly.book.ui.activity.BillboardActivity;
import com.darly.book.ui.activity.BookListActivity;
import com.darly.book.ui.activity.BookSortActivity;
import com.darly.book.ui.adapter.SectionAdapter;
import com.darly.book.ui.base.BaseFragment;
import com.darly.book.widget.itemdecoration.DividerItemDecoration;

import java.util.ArrayList;


public class FindFragment extends BaseFragment {
    /******************view************************/
    RecyclerView mRvContent;
    /*******************Object***********************/
    SectionAdapter mAdapter;

    @Override
    protected int getContentId() {
        return R.layout.fragment_find;
    }

    @Override
    protected void initView(View root) {
        mRvContent = root.findViewById(R.id.find_rv_content);
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        setUpAdapter();
    }

    private void setUpAdapter() {
        ArrayList<SectionBean> sections = new ArrayList<>();
        for (FindType type : FindType.values()) {
            sections.add(new SectionBean(type.getTypeName(), type.getIconId()));
        }

        mAdapter = new SectionAdapter();
        mRvContent.setHasFixedSize(true);
        mRvContent.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvContent.addItemDecoration(new DividerItemDecoration(getContext()));
        mRvContent.setAdapter(mAdapter);
        mAdapter.addItems(sections);
    }


    @Override
    protected void initClick() {
        mAdapter.setOnItemClickListener(
                (view, pos) -> {
                    FindType type = FindType.values()[pos];
                    //跳转
                    switch (type) {
                        case TOP:
                            startActivity(new Intent(getContext(), BillboardActivity.class));
                            break;
                        case SORT:
                            startActivity(new Intent(getContext(), BookSortActivity.class));
                            break;
                        case TOPIC:
                            startActivity(new Intent(getContext(), BookListActivity.class));
                            break;
                    }
                }
        );

    }
}
